﻿using System;
using System.Text.RegularExpressions;

namespace Neudesic.Pulse.Entities
{
    public partial class Activity
    {

        /// <summary>
        /// Gets the is badge activity.
        /// </summary>
        /// <value>The is badge activity.</value>
        public bool IsBadge
        {
            get { return Regex.IsMatch(Type, "badge"); }
        }

        /// <summary>
        /// Gets the is create activity.
        /// </summary>
        /// <value>The is create activity.</value>
        public bool IsCreate
        {
            get { return Regex.IsMatch(Type, "create"); }
        }

        /// <summary>
        /// Gets the is comment.
        /// </summary>
        /// <value>The is comment.</value>
        public bool IsComment
        {
            get { return Type == "comment"; }
        }

        /// <summary>
        /// Gets the is picture sharing.
        /// </summary>
        /// <value>The is picture sharing.</value>
        public bool IsPictureSharing
        {
            get { return Caption == null && Description == null; }
        }

        /// <summary>
        /// Gets the is question.
        /// </summary>
        /// <value>The is question.</value>
        public bool IsQuestion
        {
            get { return Type == "question"; }
        }

        /// <summary>
        /// Gets the is answer.
        /// </summary>
        /// <value>The is answer.</value>
        public bool IsAnswer
        {
            get { return Type == "answer"; }
        }

        /// <summary>
        /// Gets the is file sharing.
        /// </summary>
        /// <value>The is file sharing.</value>
        public bool IsFileSharing
        {
            get { return Type == "file"; }
        }

        /// <summary>
        /// Gets the is link sharing.
        /// </summary>
        /// <value>The is link sharing.</value>
        public bool IsLinkSharing
        {
            get { return Caption != null && Link != null; }
        }

        /// <summary>
        /// Gets the is picture or link with image.
        /// </summary>
        /// <value>The is picture or link with image.</value>
        public bool IsPictureOrLinkWithImage
        {
            get { return (IsLinkSharing || IsPictureSharing) && Picture != null; }
        }

        /// <summary>
        /// Gets the contains link.
        /// </summary>
        /// <value>The contains link.</value>
        public bool ContainsLink
        {
            get { return ((!IsAutomated  && (Picture != null || IsFileSharing)) || this.IsLinkSharing) && this.IsAutomated == false; }
        }

        /// <summary>
        /// Gets the is automated.
        /// </summary>
        /// <value>The is automated.</value>
        public bool IsAutomated
        {
            get
            {
                return IsPage || IsUser || IsSystem || IsTag;
            }
        }

        // TODO: Enums
        /// <summary>
        /// Gets the is page.
        /// </summary>
        /// <value>The is page.</value>
        public bool IsPage
        {
            get
            {
                return Regex.IsMatch(Type, "page");
            }
        }

        /// <summary>
        /// Gets the is user.
        /// </summary>
        /// <value>The is user.</value>
        public bool IsUser
        {
            get
            {
                return Regex.IsMatch(Type, "user");
            }
        }

        /// <summary>
        /// Gets the is system.
        /// </summary>
        /// <value>The is system.</value>
        public bool IsSystem
        {
            get
            {
                return Regex.IsMatch(Type, "system");
            }
        }

        /// <summary>
        /// Gets the is tag.
        /// </summary>
        /// <value>The is tag.</value>
        public bool IsTag
        {
            get
            {
                return Regex.IsMatch(Type, "tag");
            }
        }

        /// <summary>
        /// Gets the relative time.
        /// </summary>
        /// <value>The relative time.</value>
        public string RelativeTime
        {
            get
            {

                TimeSpan timeFromNow = DateTime.UtcNow - CreatedUtc;

                if (timeFromNow.CompareTo(TimeSpan.FromMinutes(5)) < 0)
                {
                    return "just now";
                }
                else if (timeFromNow.CompareTo(TimeSpan.FromHours(1)) < 0)
                {
                    int minutes = Convert.ToInt32(timeFromNow.TotalMinutes);
                    return string.Format("{0} minute{1} ago", minutes, 1 != minutes ? "s" : string.Empty);
                }
                else if (timeFromNow.CompareTo(TimeSpan.FromDays(2)) < 0)
                {
                    int hours = Convert.ToInt32(timeFromNow.TotalHours);
                    return string.Format("{0} hour{1} ago", hours, 1 != hours ? "s" : string.Empty);
                }
                else if (timeFromNow.CompareTo(TimeSpan.FromDays(90)) < 0)
                {
                    int days = Convert.ToInt32(timeFromNow.TotalDays);
                    return string.Format("{0} day{1} ago", days, 1 != days ? "s" : string.Empty);
                }
                else
                {
                    return CreatedUtc.ToLongDateString();
                }
            }
        }
        
    }
}
